package com.peoit.honeycomb.dto.security;

import com.google.common.base.Function;
import com.google.common.collect.Collections2;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.peoit.honeycomb.domain.security.Permission;
import com.peoit.honeycomb.domain.security.Role;
import com.peoit.honeycomb.dto.BaseDto;
import org.apache.commons.lang3.StringUtils;

import java.util.*;

/**
 * 角色dto
 * Created by zhouqiu on 2015/10/15.
 */
public class RoleDto extends BaseDto {
    //权限名称
    private String name;
    //描述
    private String description;

    private Set<PermissionDto> permissions = new HashSet<PermissionDto>();

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Set<PermissionDto> getPermissions() {
        return permissions;
    }

    public void setPermissions(Set<PermissionDto> permissions) {
        this.permissions = permissions;
    }

    public RoleDto toDto(Role role){
        RoleDto dto = new RoleDto();
        dto.setId(role.getId());
        dto.setName(role.getName());
        dto.setDescription(role.getDescription());
        Set<PermissionDto> sets = Sets.newHashSet();
        for(Permission permission : role.getPermissions()){
            PermissionDto permissionDto = new PermissionDto();
            PermissionDto permissionDto1 = permissionDto.toDto(permission);
            sets.add(permissionDto1);
        }
        dto.setPermissions(sets);
        return dto;
    }

    public Role toEntity(RoleDto dto,Set<Permission> permissions){
        Role role = new Role();
        role.setId(dto.getId());
        if(StringUtils.isNotBlank(dto.getName()))role.setName(dto.getName());
        if(StringUtils.isNotBlank(dto.getDescription()))role.setDescription(dto.getDescription());
        role.setPermissions(permissions);
        return  role;
    }

    public List<RoleDto> toPageDto(List<Role> roleList){
        Collection collection = Collections2.transform(roleList, new Function<Role, RoleDto>() {
            @Override
            public RoleDto apply(Role input) {
                return toDto(input);
            }
        });

        ArrayList<RoleDto> roleDtos = Lists.newArrayList(collection);
        return  roleDtos;
    }
}
